import 'package:flutter/widgets.dart';
import 'image_animation.dart';

/// Gif 图标
class ImagesGif extends StatefulWidget {
  final AnimationController controller;
  final double height;
  final double width;
  final Duration duration;
  final int lowIndex;
  final int highIndex;
  final String path;
  final String type;
  final double opacity;

  ImagesGif(
      {Key key,
      this.controller,
      this.width = 48,
      this.height = 48,
      this.duration,
      this.lowIndex = 1,
      this.highIndex = 2,
      this.type = 'repeat',
      this.opacity = 1,
      @required this.path})
      : super(key: key);

  @override
  State<StatefulWidget> createState() => _ImagesGifState();
}

class _ImagesGifState extends State<ImagesGif> with TickerProviderStateMixin {
  AnimationController _controller;
  @override
  void initState() {
    super.initState();
    _controller = widget.controller ??
        AnimationController(
            vsync: this, duration: widget.duration ?? Duration(seconds: 1));
    _setControllerType(widget.type);
  }

  // 设置控制动画类型
  _setControllerType(String type) {
    switch (type) {
      case 'forward':
        _controller.forward();
        break;
      case 'repeat':
      default:
        _controller.repeat();
    }
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  Widget build(BuildContext context) {
    return Opacity(
        opacity: widget.opacity,
        child: ImagesAnimation(
            controller: _controller,
            w: widget.width,
            h: widget.height,
            entry: ImagesAnimationEntry(
                widget.lowIndex, widget.highIndex, '${widget.path}/%s.png')));
  }
}
